package com.nbjtjc.safe.service.cost;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostInjuryClaimMapper;
import com.nbjtjc.safe.mapper.cost.CostInjuryClaimRecordMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.cost.CostInjuryClaim;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CostInjuryClaimService {
	@Autowired
	  private CostInjuryClaimMapper costInjuryClaimMapper;
	@Autowired
	  private CostInjuryClaimRecordMapper costInjuryClaimRecordMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    
	  public Result add(CostInjuryClaim costInjuryClaim){
		        Result result=new Result();
		        if(ObjectUtil.isNull(costInjuryClaim)
		        		){		            
		        	result.setFalse("新增内容不能为空");
		            return result;
		        }
//主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(costInjuryClaim.getId())|| ObjectUtil.isNull(costInjuryClaimMapper.selectByPrimaryKey(costInjuryClaim.getId()))){
			        //同样年份一直能有一个
			        if(ObjectUtil.isNotNull(costInjuryClaim.getYear())){
			        	CostInjuryClaim costInjuryClaimYear=new CostInjuryClaim();
			        	costInjuryClaimYear.setYear(costInjuryClaim.getYear());
			        	costInjuryClaimYear.setCompanyId(MyShiroUtils.getCompanyId());

			        	if(CollectionUtil.isNotEmpty(costInjuryClaimMapper.search(costInjuryClaimYear))){
				        	result.setFalse("该年份已有受伤获赔记录，不能重复增加");
				            return result;
			        	}
			        }else{
			        	result.setFalse("年份不能为空");
			            return result;
			        }
		        	
		        	int  companeyId= MyShiroUtils.getCompanyId();
		        	costInjuryClaim.setCompanyId(companeyId);
		        	costInjuryClaim.setCompanyName(sysDeptMapper.findNameById(companeyId));
		        	costInjuryClaim.setCreateDate(new Date());
		        	costInjuryClaim.setCreateId(MyShiroUtils.getSessionUserId());
		        	int res=costInjuryClaimMapper.insertSelective(costInjuryClaim);
			        if(res==0){
			            result.setFalse("新建受伤索赔表失败");
			            return result;
			        }
		        }else
		        {
		        	costInjuryClaim.setModifyDate(new Date());
		        	costInjuryClaim.setModifyId(MyShiroUtils.getSessionUserId());
		        	int res=costInjuryClaimMapper.updateByPrimaryKeySelective(costInjuryClaim);
		        	 if(res==0){
		        		 result.setFalse("更新受伤索赔表失败");
				         return result;

		        	 }
		        }
		        result.setData(costInjuryClaim);

		        return result;
		    }


	  
	  public Result delete(int id){
	      Result result=new Result();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = costInjuryClaimMapper.deleteByPrimaryKey(id);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      return result;
	  }
	  
	  public Result delete(CostInjuryClaim costInjuryClaim){
	      Result result=new Result();
	      int id = costInjuryClaim.getId();
	      if(ObjectUtil.isNull(id)){
	          result.setFalse("作废对象不能为空");
	          return result;
	      }
	      costInjuryClaim.setCancel(1);
	      int res = costInjuryClaimMapper.updateByPrimaryKeySelective(costInjuryClaim);
	      if(res==0){
	    	  result.setFalse("作废失败");
		      return result;
	
	      }
	      costInjuryClaimRecordMapper.cancelByClaimId(costInjuryClaim.getId());
	      return result;
	  }
	  
	  public Result find(CostInjuryClaim costInjuryClaim) {
	      Result result = new Result();
	      PageHelper.startPage(costInjuryClaim.getPageCurrent(), costInjuryClaim.getPageSize());
	      List<CostInjuryClaim> costInjuryClaims = costInjuryClaimMapper.search(costInjuryClaim);
	      result.setData(new PageInfo<>(costInjuryClaims));

	      return result;
	  }
	  
	  public Result findDetail(CostInjuryClaim costInjuryClaim) {
	      Result result = new Result();
	      PageHelper.startPage(costInjuryClaim.getPageCurrent(), costInjuryClaim.getPageSize());
	      List<CostInjuryClaim> costInjuryClaims = costInjuryClaimMapper.searchDetail(costInjuryClaim);
	      result.setData(new PageInfo<>(costInjuryClaims));

	      return result;
	  }
	  
	  public Result show(int id) {
	      Result result = new Result();
	      CostInjuryClaim costInjuryClaim = costInjuryClaimMapper.show(id);
	      result.setData(costInjuryClaim);

	      return result;
	  }
	  
	  public Result searchByYear(Date year) {
	      Result result = new Result();
	      if(ObjectUtil.isNull(year)){
	    	  result.setFalse("年份不能为空");
		      return result;
	      }
	      List<CostInjuryClaim> costInjuryClaims = costInjuryClaimMapper.searchDetailByYear(year,MyShiroUtils.getCompanyId());
	      if(CollectionUtil.isEmpty(costInjuryClaims)){
	    	  result.setFalse("无该年份受伤获赔记录");
		      return result;
	      }else if(costInjuryClaims.size()>1){
	    	  result.setFalse("该年份有多个受伤获赔记录，请检查");
		      return result;
	      }else{
		      result.setData(costInjuryClaims.get(0));
	      }

	      return result;
	  }
}
